<template>
    <div class="user">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="姓名">
                <el-input v-model="formInline.user" placeholder="姓名"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
                <el-button type="primary" @click="add">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" style="width: 80%" border>
            <el-table-column label="日期">
                <template slot-scope="scope">
                    <i class="el-icon-time"></i>
                    <span style="margin-left: 10px">{{ scope.row.date }}</span>
                </template>
            </el-table-column>
            <el-table-column label="姓名">
                <template slot-scope="scope">
                    <el-popover trigger="hover" placement="top">
                        <p>姓名: {{ scope.row.name }}</p>
                        <p>住址: {{ scope.row.address }}</p>
                        <div slot="reference" class="name-wrapper">
                            <el-tag size="medium">{{ scope.row.name }}</el-tag>
                        </div>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="地址" :formatter="formatter"> </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background layout="prev, pager, next" :total="1000"> </el-pagination>
        <el-dialog title="查询用户信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
                <el-form-item label="名字">
                    <el-input v-model="formLabelAlign.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="formLabelAlign.region" disabled></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
            </span>
        </el-dialog>
        <el-dialog title="新增用户信息" :visible.sync="dialogAdd" width="30%" :before-close="handleClose">
            <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
                <el-form-item label="名字">
                    <el-input v-model="formLabelAlign.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="formLabelAlign.region"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogAdd = false">取 消</el-button>
                <el-button type="primary" @click="sure">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            formInline: {
                user: '',
                region: ''
            },
            tableData: [
                {
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                },
                {
                    date: '2016-05-04',
                    name: '李小二',
                    address: '二仙桥'
                },
                {
                    date: '2016-05-01',
                    name: '张大彪',
                    address: '成华大道'
                },
                {
                    date: '2016-05-03',
                    name: '李四',
                    address: '桥洞'
                }
            ],
            dialogVisible: false,
            dialogAdd: false,
            labelPosition: 'right',
            formLabelAlign: {
                name: '',
                region: '',
                type: ''
            }
        };
    },
    methods: {
        onSubmit() {
            if (this.formInline.user == '') {
                this.$message('内容不能为空');
            } else {
                for (let i = 0; i < this.tableData.length; i++) {
                    if (this.formInline.user == this.tableData[i].name) {
                        this.dialogVisible = true;
                        this.formLabelAlign.name = this.tableData[i].name;
                        this.formLabelAlign.region = this.tableData[i].address;
                    }
                }
            }

            this.formInline.user = '';
        },
        handleEdit(index, row) {
            console.log(index, row);
        },
        handleDelete(index, row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.tableData.splice(index, 1);
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        formatter(row, column) {
            return row.address;
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then((_) => {
                    done();
                })
                .catch((_) => {});
        },
        add() {
            this.dialogAdd = true;
            this.formLabelAlign.name = '';
            this.formLabelAlign.region = '';
        },
        sure() {
            if (this.formLabelAlign.name == '' || this.formLabelAlign.region == '') {
                this.$message('内容不能为空');
            } else {
                this.tableData.push({
                    date: '2016-05-02',
                    name: this.formLabelAlign.name,
                    address: this.formLabelAlign.region
                });
            }
            this.dialogAdd = false;
        },
        open() {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        }
    }
};
</script>

<style scoped lang="scss">
.user {
    margin-top: 20px;
    .el-table {
        margin: auto;
    }
    .el-pagination {
        margin-top: 20px;
    }
}
</style>
